package com.yb0os1.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

public class SHA256Utils {
    public static String sha256(String plainString) {
        String cipherString = null;
        try {
            // 获取实例（SHA-512同理）
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            // 计算摘要
            byte[] cipherBytes = messageDigest.digest(plainString.getBytes(StandardCharsets.UTF_8));
            // 输出为16进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : cipherBytes) {
                sb.append(String.format("%02x", b));
            }
            cipherString = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cipherString;
    }


}
